﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace BookNet
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel p = (Panel)DetailsView1.FindControl("Panel1");
                p.Visible = false;
            }
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            // Get a reference to the currently logged on user
            MembershipUser currentUser = Membership.GetUser();

            // Determine the currently logged on user's UserId value
            Guid currentUserId = (Guid)currentUser.ProviderUserKey;

            // Assign the currently logged on user's UserId to the @UserId parameter
            e.Command.Parameters["@Seller"].Value = currentUserId;            
        }

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            // Get a reference to the currently logged on user
            MembershipUser currentUser = Membership.GetUser();

            // Determine the currently logged on user's UserId value
            Guid currentUserId = (Guid)currentUser.ProviderUserKey;

            // Assign the currently logged on user's UserId to the @UserId parameter
            e.Command.Parameters["@Seller"].Value = currentUserId;
            e.Command.Parameters["@TimeAdded"].Value = DateTime.Now;
            System.Threading.Thread.Sleep(3000);
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            Response.Redirect("~/Books.aspx");
        }

        public void FetchISBN(string isbn)
        {
            System.Threading.Thread.Sleep(1000);
            Chilkat.Xml xml0 = new Chilkat.Xml();
            //  The Chilkat XML component is freeware.

            Chilkat.Xml xml = null;
            xml = xml0.HttpGet("http://isbndb.com/api/books.xml?access_key=XHKPM3GT&results=texts&index1=isbn&value1=" + isbn);
            if (xml == null)
            {
                return;
            }
            xml.FirstChild2();
            xml.FirstChild2();
            TextBox TitleBox = (TextBox)DetailsView1.FindControl("TextBox3");
            TextBox AuthorBox = (TextBox)DetailsView1.FindControl("TextBox4");
            TextBox DescriptionBox = (TextBox)DetailsView1.FindControl("TextBox1");
            TextBox PrcieBox = (TextBox)DetailsView1.FindControl("TextBox5");
            TitleBox.Text = xml.GetChildContent("Title");
            AuthorBox.Text = xml.GetChildContent("AuthorsText");
            DescriptionBox.Text = xml.GetChildContent("Summery");
            DescriptionBox.Text += " " + xml.GetChildContent("Notes");
            if (TitleBox.Text.Equals(""))
                TitleBox.Text = "No record found...";
            if (AuthorBox.Text.Equals(""))
                AuthorBox.Text = "No record found...";
            if (DescriptionBox.Text.Equals(" "))
                DescriptionBox.Text = "No record found...";
            //xml = xml0.HttpGet("http://isbndb.com/api/books.xml?access_key=XHKPM3GT&results=prices&index1=isbn&value1=" + isbn);
            //if (xml == null)
            //{
            //    return;
            //}
            //xml.FirstChild2();
            //xml.FirstChild2();
            //Chilkat.Xml xml2 = null;
            //xml2 = xml.FindChild("Prices");
            //PrcieBox.Text += xml2.GetAttrValue("Price");

            //Giving up showing price here

            //Get cover page from librarything.com
            Panel p = (Panel)DetailsView1.FindControl("Panel1");
            Image i = (Image)DetailsView1.FindControl("Image1");
            TextBox ImageBox = (TextBox)DetailsView1.FindControl("TextBox6");
            p.Visible = true;
            i.ImageUrl = "http://covers.librarything.com/devkey/836d61b9d5c1b35b7dce31fb09cf3754/medium/isbn/" + isbn;
            ImageBox.Text = i.ImageUrl;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            TextBox t = (TextBox)DetailsView1.FindControl("TextBox2");
            FetchISBN(t.Text);
        }
    }
}
